package com.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadVolatileSynchronized011 {
    volatile int count = 0;

    static CountDownLatch end = new CountDownLatch(10);

    // volatile 只保证可见性，不保证原子性  count++ 两个原子操作
    /*synchronized*/  void m() {
        for (int i = 0; i < 1000; ++i) {
            count++;
        }
    }

    public static void main(String[] args) {
        ThreadVolatileSynchronized011 t = new ThreadVolatileSynchronized011();
        for (int i = 0; i < 10; ++i) {
            new Thread(() -> {
                t.m();
                end.countDown();
            }).start();
        }

        try {
            end.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.count);
    }

}
